#include "analysistab.h"
#include "ui_analysistab.h"

AnalysisTab::AnalysisTab(QWidget *parent) :
    QWidget(parent), m_ui(new Ui::AnalysisTab),
    imgL(0), sCounter(new SeedCounter)
{    
    m_ui->setupUi(this);
    connect(m_ui->showButton, SIGNAL(clicked()), this, SLOT(analysis()));
}

AnalysisTab::~AnalysisTab()
{
    delete m_ui;
    if(sCounter) delete sCounter;
}

void AnalysisTab::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

/*
 *  addLoader add image loader to enable image loading as well as
 *  image scrolling
 *
 */
void AnalysisTab::addLoader(ImageLoader *loader)
{
    // If loader is not null, add it to gridlayout for display
    if(loader)
    {
        m_ui->gridLayout->addWidget(loader, 0, 0);
        imgL = loader;
    }
}

/*
 *  When analysistab is activated, reload image loader for display
 *
 */
void AnalysisTab::showContent()
{
    if(imgL != 0)
        m_ui->gridLayout->addWidget(imgL, 0, 0);
}

/*
 *  Slot analysis() is activated when user has clicked on 'show analysis'
 *  Add current image file in image loader and pass it to seed counter
 *  to measure seeds' average characteristics: area, perimeter, major
 *  axis, minor axis, roundness factor, aspect ratio. Then display
 *  calculated information on analysis tab.
 *
 */
void AnalysisTab::analysis()
{
    if(imgL)
    {
        // Clear any result image currently in image loader
        imgL->clearDisplay();
        // Current image file displayed in left side of image loader
        QString file = imgL->getCurrFile();
        if(file.length() > 0)
        {
            // 'file' is of particular type in which both lid and body
            // petri dishes are visible in image. When finished, add
            // calculated results to the display
            if(sCounter->addFile(file))
            {
                m_ui->areaLabel->setText(QString::number(sCounter->avgArea()));
                m_ui->periLabel->setText(QString::number(sCounter->avgPeri()));
                m_ui->majorLabel->setText(QString::number(sCounter->avgMajor()));
                m_ui->minorLabel->setText(QString::number(sCounter->avgMinor()));
                m_ui->roundnessLabel->setText(QString::number(sCounter->avgRoundness()));
                m_ui->aspectLabel->setText(QString::number(sCounter->avgAspect()));
                m_ui->comboBox->clear();

                imgL->addResultImg(sCounter->getResultImg());
            }
            else
            {
                QMessageBox msgBox;
                msgBox.setText("This type of experiment is not supported!  ");
                msgBox.exec();
            }
        }
    }
}
